//
//  ConsumerTabBarController.swift
//  iModern
//
//  Created by Ralf on 15/5/10.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
//import SingleLineShakeAnimation

class ConsumerTabBarController: UITabBarController
{
    var activityViewController:     ActivityViewController?
    var marketViewController:       MarketViewController?
    var contactViewController:      ContactViewController?
    var unregisteredViewController: UnregisteredViewController?
    var profileViewController:      MyProfileViewController?
    var shopViewController:         ShopViewController?
    var yzShopViewController:         YZShopViewContorller?
    var communityViewController:         UIViewController?
    var createMenuViewController:   CreateMenuViewController?

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    override func viewDidLoad()
    {
        super.viewDidLoad()

        initUserTabBar()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSuccessful:", name: "Login Successful", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "logout:", name: "Logout", object: nil)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    func initActivityViewController()
    {
        if (self.activityViewController == nil)
        {
            self.activityViewController = ActivityViewController()
            let item = self.activityViewController!.tabBarItem
            item.image = UIImage(named: "Activity-Black-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Activity-Color-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
//            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "发现"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.activityViewController!)
    }

    func initMarketViewControllerController()
    {
        if (self.marketViewController == nil)
        {
            self.marketViewController = MarketViewController()
            let item = self.marketViewController!.tabBarItem
            item.image = UIImage(named: "Market-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Market-Selected-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
//            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "广场"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.marketViewController!)
    }

    func initCreateMenuViewController()
    {
        if (self.createMenuViewController == nil)
        {
            self.createMenuViewController = CreateMenuViewController()
            let item = self.createMenuViewController!.tabBarItem
            item.image = UIImage(named: "Market-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Market-Selected-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
//            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "发布"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }

        self.addChildViewController(self.createMenuViewController!)
    }

    func initMessageViewController()
    {
        if (self.contactViewController == nil)
        {
            self.contactViewController = ContactViewController()
            let item = self.contactViewController!.tabBarItem
            item.image = UIImage(named: "Message-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Message-Selected-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
//            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "消息"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.contactViewController!)
    }

    func initUnregisteredViewController()
    {
        if (self.unregisteredViewController == nil)
        {
            self.unregisteredViewController = UnregisteredViewController()
            let item = self.unregisteredViewController!.tabBarItem
            item.image = UIImage(named: "Home-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Home-Selected-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
//            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "我"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.unregisteredViewController!)
    }

    func initProfileViewController()
    {
        if self.profileViewController == nil
        {
            self.profileViewController = MyProfileViewController()
            let item = self.profileViewController!.tabBarItem
            item.image = UIImage(named: "Home-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Home-Selected-Spotlight-40.png")!.imageWithRenderingMode(.AlwaysOriginal)
//            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "我"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.profileViewController!)
    }

    func initShopViewController()
    {
        if shopViewController == nil
        {
            shopViewController = ShopViewController()

            let item = self.shopViewController!.tabBarItem
            item.image = UIImage(named: "Shop-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Shop-Selected-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            //            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "匠铺"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.shopViewController!)
    }
    
    func initYZShopViewController()
    {
        if yzShopViewController == nil
        {
            yzShopViewController = YZShopViewContorller()
            
            let item = self.yzShopViewController!.tabBarItem
            item.image = UIImage(named: "Shop-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Shop-Selected-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            //            item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            item.title = "有赞商城"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.yzShopViewController!)
    }
    
    func initUMCommunityViewController()
    {
        /*
        if communityViewController == nil
        {
            communityViewController = UMCommunity.getFeedsViewController()
            
            let item = self.communityViewController!.tabBarItem
            item.image = UIImage(named: "Message-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "Message-Selected-Spotlight-40.png")?.imageWithRenderingMode(.AlwaysOriginal)
            item.title = "社区"
            let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.blackColor()]
            item.setTitleTextAttributes(attributes, forState: .Highlighted)
        }
        self.addChildViewController(self.communityViewController!)
        */
    }

    func login()
    {
        self.unregisteredViewController?.removeFromParentViewController()
        self.initProfileViewController()
    }

    func logout()
    {
        self.profileViewController?.removeFromParentViewController()
        self.initUnregisteredViewController()
    }

    //    self.unregisteredViewController = [UnregisteredViewController new];
    //    self.unregisteredViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    //    self.unregisteredViewController.tabBarItem.tag = 3;
    //    self.unregisteredViewController.tabBarItem.image = [[UIImage imageNamed:@"Profile-Black-Spotlight-40.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    self.unregisteredViewController.tabBarItem.selectedImage = [[UIImage imageNamed:@"Profile-Color-Spotlight-40.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //
    //    self.profileViewController = [ProfileViewController new];
    //    self.profileViewController.tabBarItem.image = [[UIImage imageNamed:@"Profile-Black-Spotlight-40.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    self.profileViewController.tabBarItem.selectedImage = [[UIImage imageNamed:@"Profile-Color-Spotlight-40.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    self.profileViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    //    self.profileViewController.tabBarItem.tag = 3;

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)
    {
    }

    @IBAction func logout(sender: AnyObject?)
    {
        self.profileViewController?.removeFromParentViewController()
        self.initUnregisteredViewController()
        self.selectedIndex = 0
        self.navigationController?.popToRootViewControllerAnimated(true)
    }

    @IBAction func loginSuccessful(sender: AnyObject?)
    {
        self.unregisteredViewController?.removeFromParentViewController()
        self.initProfileViewController()
        self.selectedIndex = 0
    }

    func initUserTabBar()
    {
        //initYZShopViewController() //暂时不显示有赞微商城
        initMarketViewControllerController()

//        initSocialViewController()
//        initCreateMenuViewController()
        initMessageViewController()
        //友盟微社区
        //initUMCommunityViewController()
        initShopViewController()
        if appDelegate.isLogged()
        {
            self.initProfileViewController()
        }
        else
        {
            self.initUnregisteredViewController()
        }

        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}
